// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.query;

import java.util.ArrayList;
import java.util.List;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.model.database.DBTable;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.ui.i18n.Messages;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.SearchPattern;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-15 qiang.zhang $
 * 
 */
public class TablesSelectDialog extends TitleAreaDialog {

    private final DatabaseDiagram dbDiagram;

    private CheckboxTreeViewer zViewer;

    private final ViewerFilter filter = new ViewerFilter() {

        /*
         * (non-Javadoc)
         * 
         * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object,
         * java.lang.Object)
         */
        @Override
        public boolean select(Viewer viewer, Object parentElement, Object element) {
            if (patternText == null || !patternText.isEnabled() || patternText.getText() == null
                    || "".equals(patternText.getText())) { //$NON-NLS-1$
                return true;
            } else {
                if (element instanceof DBTable) {
                    DBTable table = (DBTable) element;
                    SearchPattern searchPattern = new SearchPattern();
                    searchPattern.setPattern(patternText.getText());
                    return searchPattern.matches(table.getName());
                }
            }
            return true;
        }
    };

    private Text patternText;

    private List<DBTable> selectedTables;

    /**
     * Qiang.Zhang.Adolf@gmail.com TablesSelectDialog constructor comment.
     * 
     * @param parentShell
     */
    public TablesSelectDialog(Shell parentShell, DatabaseDiagram dbDiagram) {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE | SWT.DIALOG_TRIM | SWT.MAX);
        this.dbDiagram = dbDiagram;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.dialogs.SelectionStatusDialog#configureShell(org.eclipse.swt.widgets.Shell)
     */
    @Override
    protected void configureShell(Shell shell) {
        super.configureShell(shell);
        shell.setText(Messages.getString("TablesSelectDialog.selectTC")); //$NON-NLS-1$
        shell.setSize(350, 500);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Composite clientArea = (Composite) super.createDialogArea(parent);
        zViewer = QueryUiUtils.getInstance().createDbDiagramCheckBoxTree(clientArea, dbDiagram, true);
        zViewer.setAllChecked(true);
        zViewer.addFilter(filter);
        Group group = new Group(clientArea, SWT.NONE);
        group.setText(Messages.getString("TablesSelectDialog.options")); //$NON-NLS-1$
        GridLayout gridLayout = new GridLayout();
        group.setLayout(gridLayout);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        group.setLayoutData(gridData);
        final Button usePattern = new Button(group, SWT.CHECK);
        usePattern.setText(Messages.getString("TablesSelectDialog.usePattern")); //$NON-NLS-1$

        Composite composite = new Composite(group, SWT.NONE);
        gridLayout = new GridLayout(2, false);
        composite.setLayout(gridLayout);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        composite.setLayoutData(gridData);
        Label label = new Label(composite, SWT.NONE);
        label.setText(Messages.getString("TablesSelectDialog.tPattern")); //$NON-NLS-1$
        patternText = new Text(composite, SWT.BORDER);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        patternText.setLayoutData(gridData);
        patternText.setEnabled(false);
        setTitle(Messages.getString("TablesSelectDialog.sTaCol")); //$NON-NLS-1$
        setMessage(Messages.getString("TablesSelectDialog.chooseTs")); //$NON-NLS-1$
        setTitleImage(ImageUtil.getImage(IDenguesSharedImage.WIZARD_TABLES));
        usePattern.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                patternText.setEnabled(usePattern.getSelection());
            }
        });
        patternText.addModifyListener(new ModifyListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
             */
            public void modifyText(ModifyEvent e) {
                zViewer.refresh();
            }
        });
        return clientArea;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.Dialog#okPressed()
     */
    @Override
    protected void okPressed() {
        Object[] checkedElements = zViewer.getCheckedElements();
        selectedTables = new ArrayList<DBTable>();
        for (Object object : checkedElements) {
            if (object instanceof DBTable) {
                selectedTables.add((DBTable) object);
            }
        }
        super.okPressed();
    }

    /**
     * Getter for selectedTables.
     * 
     * @return the selectedTables
     */
    public List<DBTable> getSelectedTables() {
        return this.selectedTables;
    }
}
